// miniprogram/pages/advance/advance.js
var that;
var app = getApp();
const db = wx.cloud.database();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: '',
    content: '',
    user: {},
  },
  onLoad: function (options) {
    that = this
    wx.cloud.init({
      env: 'test-5gwxfg0see31cde1',
    })
  },

  /**
   *  获取填写的内容
   */
  textArea: function (event) {
    that.data.content = event.detail.value;
    this.setData({
     content : that.data.content
    })
  },
 
  /**
   * 发布
   */
  formSubmit: function (e) {
    that.data.title = e.detail.value.input 
    console.log("提交意见")
    console.log(that.data.title)
    console.log(that.data.content)
    if (this.data.title.trim() != '') {
      this.saveDataToServer();
    } else if (this.data.content.trim() != '') {
      this.saveDataToServer();
    } else {
      wx.showToast({
        title: '填反馈意见',
      })
    }
  },
  /**
   * 保存到发布集合中
   */
  saveDataToServer: function (event) {
    db.collection('advice').add({
      data:{
        title : that.data.title,
        content : that.data.content,
        date : new Date()
      }
    })
    that.showTipAndSwitchTab();
  },
  /**
   * 添加成功添加提示，切换页面
   */
  showTipAndSwitchTab: function (event) {
    wx.showToast({
      title: '反馈成功',
    })
    setTimeout(function(){
      wx.navigateBack({
        url: '../me/me',
      })
    },1000)
   
  },
 

  
 
 
 
})